package usf.gwt.ui.bootstrap.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.ui.HasText;

public abstract class CheckableControl extends FormControl<Boolean> implements HasText {

	public CheckableControl() {
		super();
	}
	
	@Override
	protected Element initWidget() {
		Element div = Document.get().createDivElement();
		Element label = Document.get().createLabelElement();
		label.appendChild(createCheckable());
		label.appendChild(Document.get().createTextNode(""));
		div.appendChild(label);
		return div;
	}
	
	public abstract InputElement createCheckable();
	
	@Override
	protected InputElement getBaseElement() {
		return getLabelElement().getFirstChild().cast();
	}
	protected Element getLabelElement() {
		return getElement().getFirstChildElement();
	}
	protected Element getContentElement() {
		return getLabelElement().getLastChild().cast();
	}
	
	@Override
	public String getText() {
		return getContentElement().getNodeValue();
	}
	@Override
	public void setText(String text) {
		getContentElement().setNodeValue(text);
	}

	public String getColor() {
		return getLabelElement().getStyle().getColor();
	}
	public void setColor(String color) {
		getLabelElement().getStyle().setColor(color);
	}

	public void setChecked(boolean checked) {
		getBaseElement().setChecked(checked);
	}
	public boolean getChecked() {
		return getBaseElement().isChecked();
	}
	
	@Override
	protected Boolean getChangedValue(NativeEvent e) {
		return getChecked();
	}
}